package teleradiologia.web;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import teleradiologia.domain.Address;
import teleradiologia.domain.Customer;
import teleradiologia.domain.Diagnosticlab;
import teleradiologia.domain.Doctor;
import teleradiologia.domain.Managerdiagnosticlab;
import teleradiologia.domain.Ordertype;
import teleradiologia.domain.Person;
import teleradiologia.domain.Radiologylab;
import teleradiologia.domain.Technician;
import teleradiologia.service.AddressManipulator;
import teleradiologia.service.CustomerManipulator;
import teleradiologia.service.DiagnosticlabManipulator;
import teleradiologia.service.DiagnosysManipulator;
import teleradiologia.service.DoctorManipulator;
import teleradiologia.service.ManagerdiagnosticlabManipulator;
import teleradiologia.service.OfferManipulator;
import teleradiologia.service.OrderingManipulator;
import teleradiologia.service.OrdertypeManipulator;
import teleradiologia.service.PatientManipulator;
import teleradiologia.service.PersonManipulator;
import teleradiologia.service.PictureManipulator;
import teleradiologia.service.RadiologylabManipulator;
import teleradiologia.service.RadlabmanagerManipulator;
import teleradiologia.service.TechnicianManipulator;
import teleradiologia.service.TemplateManipulator;
import teleradiologia.utils.DoctorWarnings;
import teleradiologia.utils.LabWarnings;
import teleradiologia.utils.ManagerWarnings;
import teleradiologia.utils.TechnicianWarnings;
import teleradiologia.utils.Validation;

@Controller
public class AdminController {

	private static final Log log = LogFactory.getLog(AdminController.class);

	public void setAddressManipulator(AddressManipulator addressManipulator) {
		this.addressManipulator = addressManipulator;
	}

	public void setCustomerManipulator(CustomerManipulator customerManipulator) {
		this.customerManipulator = customerManipulator;
	}

	public void setDiagnosticlabManipulator(
			DiagnosticlabManipulator diagnosticlabManipulator) {
		this.diagnosticlabManipulator = diagnosticlabManipulator;
	}

	public void setDiagnosysManipulator(
			DiagnosysManipulator diagnosysManipulator) {
		this.diagnosysManipulator = diagnosysManipulator;
	}

	public void setDoctorManipulator(DoctorManipulator doctorManipulator) {
		this.doctorManipulator = doctorManipulator;
	}

	public void setManagerdiagnosticlabManipulator(
			ManagerdiagnosticlabManipulator managerdiagnosticlabManipulator) {
		this.managerdiagnosticlabManipulator = managerdiagnosticlabManipulator;
	}

	public void setOfferManipulator(OfferManipulator offerManipulator) {
		this.offerManipulator = offerManipulator;
	}

	public void setOrderingManipulator(OrderingManipulator orderingManipulator) {
		this.orderingManipulator = orderingManipulator;
	}

	public void setOrdertypeManipulator(
			OrdertypeManipulator ordertypeManipulator) {
		this.ordertypeManipulator = ordertypeManipulator;
	}

	public void setPatientManipulator(PatientManipulator patientManipulator) {
		this.patientManipulator = patientManipulator;
	}

	public void setPersonManipulator(PersonManipulator personManipulator) {
		this.personManipulator = personManipulator;
	}

	public void setPictureManipulator(PictureManipulator pictureManipulator) {
		this.pictureManipulator = pictureManipulator;
	}

	public void setRadiologylabManipulator(
			RadiologylabManipulator radiologylabManipulator) {
		this.radiologylabManipulator = radiologylabManipulator;
	}

	public void setRadlabmanagerManipulator(
			RadlabmanagerManipulator radlabmanagerManipulator) {
		this.radlabmanagerManipulator = radlabmanagerManipulator;
	}

	public void setTechnicianManipulator(
			TechnicianManipulator technicianManipulator) {
		this.technicianManipulator = technicianManipulator;
	}

	public void setTemplateManipulator(TemplateManipulator templateManipulator) {
		this.templateManipulator = templateManipulator;
	}

	private AddressManipulator addressManipulator;
	private CustomerManipulator customerManipulator;
	private DiagnosticlabManipulator diagnosticlabManipulator;
	private DiagnosysManipulator diagnosysManipulator;
	private DoctorManipulator doctorManipulator;
	private ManagerdiagnosticlabManipulator managerdiagnosticlabManipulator;
	private OfferManipulator offerManipulator;
	private OrderingManipulator orderingManipulator;
	private OrdertypeManipulator ordertypeManipulator;
	private PatientManipulator patientManipulator;
	private PersonManipulator personManipulator;
	private PictureManipulator pictureManipulator;
	private RadiologylabManipulator radiologylabManipulator;
	private RadlabmanagerManipulator radlabmanagerManipulator;
	private TechnicianManipulator technicianManipulator;
	private TemplateManipulator templateManipulator;

	@RequestMapping("/admin/adminHome.htm")
	public ModelAndView handleAdminHome() {
		return new ModelAndView("admin/adminHome");
	}

	@RequestMapping("/admin/manipulateRadiologylabs.htm")
	public ModelAndView handleManipulateRadiologylabs() {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("radiologylabs", this.radiologylabManipulator.getAll());

		ModelAndView result = new ModelAndView("admin/manipulateRadiologylabs",
				"model", myModel);

		return result;
	}

	@RequestMapping("/admin/addRadiologylabHome.htm")
	public ModelAndView handleAddRadiologylabHome() {
		return new ModelAndView("admin/addRadiologylabHome");
	}

	@RequestMapping("/admin/addRadiologylabHomeValidation.htm")
	public ModelAndView handleAddRadiologylabHomeValidation(
			@RequestParam("labWarnings") LabWarnings labWarnings,
			@RequestParam("radiologylab") Radiologylab radiologylab) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("radiologylab", radiologylab);
		myModel.put("labWarnings", labWarnings);

		return new ModelAndView("admin/addRadiologylabHome", "model", myModel);
	}

	@RequestMapping("/admin/addRadiologylab.htm")
	public ModelAndView handleAddRadiologylab(
			@RequestParam("addRadiologylabKrsnumber") String krsnumber,
			@RequestParam("addRadiologylabName") String name,
			@RequestParam("addRadiologylabNip") String nip,
			@RequestParam("addRadiologylabDesription") String description,
			@RequestParam("addRadiologylabTel") String tel,
			@RequestParam("addAddressCity") String city,
			@RequestParam("addAddressStreet") String street,
			@RequestParam("addAddressHouseNumber") String houseNumber,
			@RequestParam("addAddressFlatNumber") String flatNumber,
			@RequestParam("addAddressPostalCode") String postalCode) {

		Radiologylab radiologylab = new Radiologylab();
		radiologylab.setKrsnumber(krsnumber);
		radiologylab.setName(name);
		radiologylab.setNip(nip);
		radiologylab.setDescription(description);
		radiologylab.setTel(tel);

		Address address = new Address();
		address.setCity(city);
		address.setStreet(street);
		address.setHousenumber(houseNumber);
		address.setFlatnumber(flatNumber);
		address.setPostalcode(postalCode);

		LabWarnings labWarnings = new LabWarnings(Validation.validateNip(nip),
				Validation.validateKrs(krsnumber), Validation
						.validateHousenumber(houseNumber), Validation
						.validateFlatnumber(flatNumber), Validation
						.validateTel(tel), Validation
						.validatePostalCode(postalCode));

		if (labWarnings.isEmpty()) {

			this.radiologylabManipulator.createRadiologylab(radiologylab,
					address, new Customer());
			return handleManipulateRadiologylabs();
		} else {
			radiologylab.setAddress(address);
			return handleAddRadiologylabHomeValidation(labWarnings,
					radiologylab);
		}
	}

	@RequestMapping("/admin/manipulateRadlabWorkers.htm")
	public ModelAndView handleManipulateTechniciansFromRadlab(
			@RequestParam("checkedRadiologylabKrsnumber") String krsnumber) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();
		Radiologylab radiologylab = this.radiologylabManipulator
				.getByKrsnumber(krsnumber);

		myModel.put("radiologylab", radiologylab);
		myModel.put("technicians", this.technicianManipulator
				.getAllByKrsnumber(krsnumber));

		return new ModelAndView("admin/manipulateRadlabWorkers", "model",
				myModel);
	}

	@RequestMapping("/admin/manipulateDiagnosticlabWorkers.htm")
	public ModelAndView handleManipulateDoctorsFromDiagnosticlab(
			@RequestParam("checkedDiagnosticlabKrsnumber") String krsnumber) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("diagnosticlab", this.diagnosticlabManipulator
				.getByKrsnumber(krsnumber));
		myModel.put("doctors", this.doctorManipulator
				.getAllByKrsnumber(krsnumber));

		return new ModelAndView("admin/manipulateDiagnosticlabWorkers",
				"model", myModel);
	}

	@RequestMapping("/admin/radiologylabDetails.htm")
	public ModelAndView handleRadiologylabDetails(
			@RequestParam("radiologylabDetailsKrsnumber") String krsnumber) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("radiologylab", this.radiologylabManipulator
				.getByKrsnumber(krsnumber));

		ModelAndView result = new ModelAndView("admin/radiologylabDetails",
				"model", myModel);

		return result;
	}

	@RequestMapping("/admin/editRadiologylab.htm")
	public ModelAndView handleEditRadiologylab(
			@RequestParam("editRadiologylabKrsnumber") String krsnumber) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("radiologylab", this.radiologylabManipulator
				.getByKrsnumber(krsnumber));

		ModelAndView result = new ModelAndView("admin/editRadiologylab",
				"model", myModel);

		return result;
	}

	@RequestMapping("/admin/editRadiologylabValidation.htm")
	public ModelAndView handleEditRadiologylabValidation(
			@RequestParam("labWarnings") LabWarnings labWarnings,
			@RequestParam("radiologylab") Radiologylab radiologylab) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("radiologylab", radiologylab);
		myModel.put("labWarnings", labWarnings);

		ModelAndView result = new ModelAndView("admin/editRadiologylab",
				"model", myModel);

		return result;
	}

	@RequestMapping("/admin/deleteRadiologylab.htm")
	public ModelAndView handleDeleteRadiologylab(
			@RequestParam("deleteRadiologylabKrsnumber") String krsnumber) {

		this.radiologylabManipulator.delete(this.radiologylabManipulator
				.getByKrsnumber(krsnumber));

		return handleManipulateRadiologylabs();
	}

	@RequestMapping("/admin/changeRadiologylabData.htm")
	public ModelAndView handleChangeRadiologylabData(
			@RequestParam("editRadiologylabKrsnumber") String krsnumber,
			@RequestParam("editRadiologylabName") String name,
			@RequestParam("editRadiologylabNip") String nip,
			@RequestParam("editRadiologylabDescription") String description,
			@RequestParam("editRadiologylabTel") String tel,

			@RequestParam("editAddressCity") String city,
			@RequestParam("editAddressStreet") String street,
			@RequestParam("editAddressHouseNumber") String houseNumber,
			@RequestParam("editAddressFlatNumber") String flatNumber,
			@RequestParam("editAddressPostalCode") String postalCode) {

		Radiologylab radiologylab = this.radiologylabManipulator
				.getByKrsnumber(krsnumber);

		radiologylab.setName(name);
		radiologylab.setNip(nip);
		radiologylab.setDescription(description);
		radiologylab.setTel(tel);

		radiologylab.getAddress().setCity(city);
		radiologylab.getAddress().setStreet(street);
		radiologylab.getAddress().setHousenumber(houseNumber);
		radiologylab.getAddress().setFlatnumber(flatNumber);
		radiologylab.getAddress().setPostalcode(postalCode);

		LabWarnings labWarnings = new LabWarnings("", "", Validation
				.validateHousenumber(houseNumber), Validation
				.validateFlatnumber(flatNumber), Validation.validateTel(tel),
				Validation.validatePostalCode(postalCode));

		if (labWarnings.isEmpty()) {
			this.radiologylabManipulator.update(radiologylab);
			return handleManipulateRadiologylabs();
		} else {
			return handleEditRadiologylabValidation(labWarnings, radiologylab);
		}
	}

	@RequestMapping("/admin/manipulateDiagnosticlabs.htm")
	public ModelAndView handleManipulateDiagnosticlabs() {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("diagnosticlabs", this.diagnosticlabManipulator.getAll());

		ModelAndView result = new ModelAndView(
				"admin/manipulateDiagnosticlabs", "model", myModel);

		return result;
	}

	@RequestMapping("/admin/addDiagnosticlabHome.htm")
	public ModelAndView handleAddDiagnosticlabHome() {
		return new ModelAndView("admin/addDiagnosticlabHome");
	}

	@RequestMapping("/admin/addDiagnosticlabHomeValidation.htm")
	public ModelAndView handleAddDiagnosticlabHomeValidation(
			@RequestParam("labWarnings") LabWarnings labWarnings,
			@RequestParam("diagnosticlab") Diagnosticlab diagnosticlab) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("diagnosticlab", diagnosticlab);
		myModel.put("labWarnings", labWarnings);

		return new ModelAndView("admin/addDiagnosticlabHome", "model", myModel);
	}

	@RequestMapping("/admin/addDiagnosticlab.htm")
	public ModelAndView handleAddDiagnosticlab(
			@RequestParam("addDiagnosticlabKrsnumber") String krsnumber,
			@RequestParam("addDiagnosticlabName") String name,
			@RequestParam("addDiagnosticlabNip") String nip,
			@RequestParam("addDiagnosticlabDesription") String description,
			@RequestParam("addDiagnosticlabTel") String tel,
			@RequestParam("addAddressCity") String city,
			@RequestParam("addAddressStreet") String street,
			@RequestParam("addAddressHouseNumber") String houseNumber,
			@RequestParam("addAddressFlatNumber") String flatNumber,
			@RequestParam("addAddressPostalCode") String postalCode) {

		Diagnosticlab diagnosticlab = new Diagnosticlab();
		diagnosticlab.setKrsnumber(krsnumber);
		diagnosticlab.setName(name);
		diagnosticlab.setNip(nip);
		diagnosticlab.setDescription(description);
		diagnosticlab.setTel(tel);

		Address address = new Address();
		address.setCity(city);
		address.setStreet(street);
		address.setHousenumber(houseNumber);
		address.setFlatnumber(flatNumber);
		address.setPostalcode(postalCode);

		LabWarnings labWarnings = new LabWarnings(Validation.validateNip(nip),
				Validation.validateKrs(krsnumber), Validation
						.validateHousenumber(houseNumber), Validation
						.validateFlatnumber(flatNumber), Validation
						.validateTel(tel), Validation
						.validatePostalCode(postalCode));

		if (labWarnings.isEmpty()) {
			this.diagnosticlabManipulator.create(diagnosticlab, address);
			return handleManipulateDiagnosticlabs();
		} else {
			diagnosticlab.setAddress(address);
			return handleAddDiagnosticlabHomeValidation(labWarnings,
					diagnosticlab);
		}
	}

	@RequestMapping("/admin/diagnosticlabDetails.htm")
	public ModelAndView handleDiagnosticlabDetails(
			@RequestParam("diagnosticlabDetailsKrsnumber") String krsnumber) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("diagnosticlab", this.diagnosticlabManipulator
				.getByKrsnumber(krsnumber));

		ModelAndView result = new ModelAndView("admin/diagnosticlabDetails",
				"model", myModel);

		return result;
	}

	@RequestMapping("/admin/editDiagnosticlab.htm")
	public ModelAndView handleEditDiagnosticlab(
			@RequestParam("editDiagnosticlabKrsnumber") String krsnumber) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("diagnosticlab", this.diagnosticlabManipulator
				.getByKrsnumber(krsnumber));

		ModelAndView result = new ModelAndView("admin/editDiagnosticlab",
				"model", myModel);

		return result;
	}

	@RequestMapping("/admin/editDiagnosticlabValidation.htm")
	public ModelAndView handleEditDiagnosticlabValidation(
			@RequestParam("labWarnings") LabWarnings labWarnings,
			@RequestParam("diagnosticlab") Diagnosticlab diagnosticlab) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("diagnosticlab", diagnosticlab);
		myModel.put("labWarnings", labWarnings);

		ModelAndView result = new ModelAndView("admin/editDiagnosticlab",
				"model", myModel);

		return result;
	}

	@RequestMapping("/admin/deleteDiagnosticlab.htm")
	public ModelAndView handleDeleteDiagnosticlab(
			@RequestParam("deleteDiagnosticlabKrsnumber") String krsnumber) {

		this.diagnosticlabManipulator.delete(this.diagnosticlabManipulator
				.getByKrsnumber(krsnumber));

		return handleManipulateDiagnosticlabs();
	}

	@RequestMapping("/admin/changeDiagnosticlabData.htm")
	public ModelAndView handleChangeDiagnosticlabData(
			@RequestParam("editDiagnosticlabKrsnumber") String krsnumber,
			@RequestParam("editDiagnosticlabName") String name,
			@RequestParam("editDiagnosticlabNip") String nip,
			@RequestParam("editDiagnosticlabDescription") String description,
			@RequestParam("editDiagnosticlabTel") String tel,

			@RequestParam("editAddressCity") String city,
			@RequestParam("editAddressStreet") String street,
			@RequestParam("editAddressHouseNumber") String houseNumber,
			@RequestParam("editAddressFlatNumber") String flatNumber,
			@RequestParam("editAddressPostalCode") String postalCode) {

		Diagnosticlab diagnosticlab = this.diagnosticlabManipulator
				.getByKrsnumber(krsnumber);

		diagnosticlab.setName(name);
		diagnosticlab.setNip(nip);
		diagnosticlab.setDescription(description);
		diagnosticlab.setTel(tel);

		diagnosticlab.getAddress().setCity(city);
		diagnosticlab.getAddress().setStreet(street);
		diagnosticlab.getAddress().setHousenumber(houseNumber);
		diagnosticlab.getAddress().setFlatnumber(flatNumber);
		diagnosticlab.getAddress().setPostalcode(postalCode);

		LabWarnings labWarnings = new LabWarnings(Validation.validateNip(nip),
				"", Validation.validateHousenumber(houseNumber), Validation
						.validateFlatnumber(flatNumber), Validation
						.validateTel(tel), Validation
						.validatePostalCode(postalCode));

		if (labWarnings.isEmpty()) {
			this.diagnosticlabManipulator.update(diagnosticlab);
			return handleManipulateDiagnosticlabs();
		} else {
			return handleEditDiagnosticlabValidation(labWarnings, diagnosticlab);
		}
	}

	// -------------

	@RequestMapping("/admin/manipulateOrdertypes.htm")
	public ModelAndView handleManipulateOrdertypes() {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("ordertypes", this.ordertypeManipulator.getAll());

		ModelAndView result = new ModelAndView("admin/manipulateOrdertypes",
				"model", myModel);

		return result;
	}

	@RequestMapping("/admin/addOrdertypeHome.htm")
	public ModelAndView handleAddOrdertypeHome() {
		return new ModelAndView("admin/addOrdertypeHome");
	}

	@RequestMapping("/admin/addOrdertype.htm")
	public ModelAndView handleAddOrdertype(
			@RequestParam("addOrdertypeRadiologyexaminationtype") String radiologyexaminationtype,
			@RequestParam("addOrdertypeBodypart") String bodypart,
			@RequestParam("addOrdertypeBodypartdetails") String bodypartdetails) {

		Ordertype ordertype = new Ordertype();
		ordertype.setBodypart(bodypart);
		ordertype.setBodypartdetails(bodypartdetails);
		ordertype.setRadiologyexaminationtype(radiologyexaminationtype);

		this.ordertypeManipulator.create(ordertype);
		return handleManipulateOrdertypes();
	}

	@RequestMapping("/admin/editOrdertype.htm")
	public ModelAndView handleEditOrdertype(
			@RequestParam("editOrdertypeId") long id) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		Ordertype ordertype = this.ordertypeManipulator.getById(id);

		myModel.put("ordertype", ordertype);

		ModelAndView result = new ModelAndView("admin/editOrdertype", "model",
				myModel);

		return result;
	}

	@RequestMapping("/admin/deleteOrdertype.htm")
	public ModelAndView handleDeleteOrdertype(
			@RequestParam("deleteOrdertypeId") long id) {

		this.ordertypeManipulator.delete(this.ordertypeManipulator.getById(id));

		return handleManipulateOrdertypes();
	}

	@RequestMapping("/admin/changeOrdertypeData.htm")
	public ModelAndView handleChangeOrdertypeData(
			@RequestParam("editOrdertypeRadiologyexaminationtype") String radiologyexaminationtype,
			@RequestParam("editOrdertypeBodypart") String bodypart,
			@RequestParam("editOrdertypeBodypartdetails") String bodypartdetails,
			@RequestParam("editOrdertypeId") long id) {

		Ordertype ordertype = this.ordertypeManipulator.getById(id);
		ordertype.setBodypart(bodypart);
		ordertype.setBodypartdetails(bodypartdetails);
		ordertype.setRadiologyexaminationtype(radiologyexaminationtype);

		this.ordertypeManipulator.update(ordertype);
		return handleManipulateOrdertypes();
	}

	// -------------

	// Technician

	@RequestMapping("/admin/manipulateTechnicians.htm")
	public ModelAndView handleManipulateTechnicians() {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("technicians", this.technicianManipulator.getAll());

		return new ModelAndView("admin/manipulateTechnicians", "model", myModel);
	}

	@RequestMapping("/admin/addTechnicianHome.htm")
	public ModelAndView handleAddTechnicianHome() {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("radiologylabs", this.radiologylabManipulator.getAll());

		return new ModelAndView("admin/addTechnicianHome", "model", myModel);
	}

	@RequestMapping("/admin/addTechnicianHomeValidation.htm")
	public ModelAndView handleAddTechnicianHomeValidation(
			@RequestParam("technicianWarnings") TechnicianWarnings technicianWarnings,
			@RequestParam("technician") Technician technician) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("technicianWarnings", technicianWarnings);
		myModel.put("technician", technician);
		myModel.put("radiologylabs", this.radiologylabManipulator.getAll());

		return new ModelAndView("admin/addTechnicianHome", "model", myModel);
	}

	@RequestMapping("/admin/addTechnician.htm")
	public ModelAndView handleAddTechnician(
			// parametry dotyczace person
			@RequestParam("addPersonName") String name,
			@RequestParam("addPersonSurname") String surname,
			@RequestParam("addPersonLogin") String login,
			@RequestParam("addPersonPassword") String password,
			@RequestParam("addPersonEmail") String email,
			@RequestParam("addPersonEnabled") String enabled,

			// parametry dotyczace technika
			@RequestParam("addTechnicianNip") String nip,
			@RequestParam("addTechnicianRadiologyLab") String krsnumber) {

		TechnicianWarnings technicianWarnings = new TechnicianWarnings(
				Validation.validateNip(nip), Validation.validateEmail(email));

		Person person = new Person();

		person.setName(name);
		person.setSurname(surname);
		person.setLogin(login);
		person.setPassword(password);
		person.setEmail(email);
		person.setAuthority("ROLE_TECHNICIAN");

		log.info("enabled: " + enabled);
		if (enabled.equals("off")) {
			person.setEnabled(false);
		} else {
			person.setEnabled(true);
		}

		Technician technician = new Technician();
		technician.setNip(nip);

		Radiologylab radiologylab = this.radiologylabManipulator
				.getByKrsnumber(krsnumber);

		if (technicianWarnings.isEmpty()) {
			technicianManipulator.create(technician, person, radiologylab);
			return handleManipulateTechnicians();
		} else {
			technician.setPerson(person);
			return handleAddTechnicianHomeValidation(technicianWarnings,
					technician);
		}
	}

	@RequestMapping("/admin/technicianDetails.htm")
	public ModelAndView handleTechnicianDetails(
			@RequestParam("technicianDetailsNip") String nip) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("technician", this.technicianManipulator.getByNip(nip));

		return new ModelAndView("admin/technicianDetails", "model", myModel);
	}

	@RequestMapping("/admin/editTechnician.htm")
	public ModelAndView handleEditTechnician(
			@RequestParam("edittechnicianNip") String nip) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("technician", this.technicianManipulator.getByNip(nip));
		myModel.put("radiologylabs", this.radiologylabManipulator.getAll());

		return new ModelAndView("admin/editTechnician", "model", myModel);
	}

	@RequestMapping("/admin/editTechnicianValidation.htm")
	public ModelAndView handleEditTechnicianValidation(
			@RequestParam("technicianWarnings") TechnicianWarnings technicianWarnings,
			@RequestParam("technician") Technician technician) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("technicianWarnings", technicianWarnings);
		myModel.put("technician", technician);
		myModel.put("radiologylabs", this.radiologylabManipulator.getAll());

		return new ModelAndView("admin/editTechnician", "model", myModel);
	}

	@RequestMapping("/admin/deleteTechnician.htm")
	public ModelAndView handleDeleteTechnician(
			@RequestParam("deleteTechnicianNip") String nip) {

		this.technicianManipulator.delete(this.technicianManipulator
				.getByNip(nip));

		return handleManipulateTechnicians();
	}

	@RequestMapping("/admin/changeTechnicianData.htm")
	public ModelAndView handleChangeTechnicianData(
			@RequestParam("editPersonName") String name,
			@RequestParam("editPersonSurname") String surname,
			@RequestParam("editPersonLogin") String login,
			@RequestParam("editPersonEmail") String email,
			@RequestParam("editPersonEnabled") String enabled,
			@RequestParam("editTechnicianNip") String nip,
			@RequestParam("editTechnicianRadiologyLab") String krsnumber) {

		TechnicianWarnings technicianWarnings = new TechnicianWarnings(
				Validation.validateNip(nip), Validation.validateEmail(email));

		Technician technician = this.technicianManipulator.getByNip(nip);
		if (krsnumber != "")
			technician.setRadiologylab(this.radiologylabManipulator
					.getByKrsnumber(krsnumber));

		technician.getPerson().setName(name);
		technician.getPerson().setSurname(surname);
		technician.getPerson().setLogin(login);
		technician.getPerson().setEmail(email);

		if (enabled.equals("off")) {
			technician.getPerson().setEnabled(false);
		} else {
			technician.getPerson().setEnabled(true);
		}

		this.technicianManipulator.update(technician);

		if (technicianWarnings.isEmpty()) {
			this.technicianManipulator.update(technician);
			return handleManipulateTechnicians();
		} else {
			return handleEditTechnicianValidation(technicianWarnings,
					technician);
		}
	}

	// doctor

	@RequestMapping("/admin/manipulateDoctors.htm")
	public ModelAndView handleManipulateDoctors() {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("doctors", this.doctorManipulator.getAll());

		return new ModelAndView("admin/manipulateDoctors", "model", myModel);
	}

	@RequestMapping("/admin/addDoctorHome.htm")
	public ModelAndView handleAddDoctorHome() {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("diagnosticlabs", this.diagnosticlabManipulator.getAll());

		return new ModelAndView("admin/addDoctorHome", "model", myModel);
	}

	@RequestMapping("/admin/addDoctorHomeValidation.htm")
	public ModelAndView handleAddDoctorHomeValidation(
			@RequestParam("doctorWarnings") DoctorWarnings doctorWarnings,
			@RequestParam("doctor") Doctor doctor) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();
		myModel.put("doctor", doctor);
		myModel.put("diagnosticlabs", this.diagnosticlabManipulator.getAll());
		myModel.put("doctorWarnings", doctorWarnings);

		return new ModelAndView("admin/addDoctorHome", "model", myModel);
	}

	@RequestMapping("/admin/addDoctor.htm")
	public ModelAndView handleAddDoctor(
			// parametry dotyczace person
			@RequestParam("addPersonName") String name,
			@RequestParam("addPersonSurname") String surname,
			@RequestParam("addPersonLogin") String login,
			@RequestParam("addPersonPassword") String password,
			@RequestParam("addPersonEmail") String email,
			@RequestParam("addPersonEnabled") String enabled,

			// parametry dotyczace doktora
			@RequestParam("addDoctorPwz") String pwz,
			@RequestParam("addDoctorNip") String nip,
			@RequestParam("addDoctorDiagnosticlab") String krsnumber) {

		DoctorWarnings doctorWarnings = new DoctorWarnings(Validation
				.validateNip(nip), Validation.validatePwz(pwz), Validation
				.validateEmail(email));

		Person person = new Person();

		person.setName(name);
		person.setSurname(surname);
		person.setLogin(login);
		person.setPassword(password);
		person.setEmail(email);
		person.setAuthority("ROLE_DOCTOR");

		if (enabled.equals("off")) {
			person.setEnabled(false);
		} else {
			person.setEnabled(true);
		}

		Doctor doctor = new Doctor();
		doctor.setNip(nip);
		doctor.setPwz(pwz);

		Diagnosticlab diagnosticlab = this.diagnosticlabManipulator
				.getByKrsnumber(krsnumber);

		if (doctorWarnings.isEmpty()) {
			this.doctorManipulator.create(doctor, person, diagnosticlab);
			return handleManipulateDoctors();
		} else {
			doctor.setPerson(person);
			return handleAddDoctorHomeValidation(doctorWarnings, doctor);
		}
	}

	@RequestMapping("/admin/doctorDetails.htm")
	public ModelAndView handleDoctorDetails(
			@RequestParam("doctorDetailsNip") String nip) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("doctor", this.doctorManipulator.getByNip(nip));

		return new ModelAndView("admin/doctorDetails", "model", myModel);
	}

	@RequestMapping("/admin/editDoctor.htm")
	public ModelAndView handleEditDoctor(
			@RequestParam("editDoctorNip") String nip) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("doctor", this.doctorManipulator.getByNip(nip));
		myModel.put("diagnosticlabs", this.diagnosticlabManipulator.getAll());

		return new ModelAndView("admin/editDoctor", "model", myModel);
	}

	@RequestMapping("/admin/editDoctorValidation.htm")
	public ModelAndView handleEditDoctorValidation(
			@RequestParam("doctorWarnings") DoctorWarnings doctorWarnings,
			@RequestParam("doctor") Doctor doctor) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("doctorWarnings", doctorWarnings);
		myModel.put("doctor", doctor);
		myModel.put("diagnosticlabs", this.diagnosticlabManipulator.getAll());

		return new ModelAndView("admin/editDoctor", "model", myModel);
	}

	@RequestMapping("/admin/deleteDoctor.htm")
	public ModelAndView handleDeleteDoctor(
			@RequestParam("deleteDoctorNip") String nip) {

		this.doctorManipulator.delete(this.doctorManipulator.getByNip(nip));

		return handleManipulateDoctors();
	}

	@RequestMapping("/admin/changeDoctorData.htm")
	public ModelAndView handleChangeDoctorData(
			@RequestParam("editPersonName") String name,
			@RequestParam("editPersonSurname") String surname,
			@RequestParam("editPersonLogin") String login,
			@RequestParam("editPersonEmail") String email,
			@RequestParam("editPersonEnabled") String enabled,

			@RequestParam("editDoctorPwz") String pwz,
			@RequestParam("editDoctorNip") String nip,
			@RequestParam("editDoctorDiagnosticlab") String krsnumber) {

		DoctorWarnings doctorWarnings = new DoctorWarnings(Validation
				.validateNip(nip), Validation.validatePwz(pwz), Validation
				.validateEmail(email));

		Doctor doctor = this.doctorManipulator.getByNip(nip);
		doctor.setNip(nip);
		doctor.setPwz(pwz);
		doctor.setDiagnosticlab(this.diagnosticlabManipulator
				.getByKrsnumber(krsnumber));

		doctor.getPerson().setName(name);
		doctor.getPerson().setSurname(surname);
		doctor.getPerson().setLogin(login);
		doctor.getPerson().setEmail(email);

		if (enabled.equals("off")) {
			doctor.getPerson().setEnabled(false);
		} else {
			doctor.getPerson().setEnabled(true);
		}
		if (doctorWarnings.isEmpty()) {
			this.doctorManipulator.update(doctor);
			return handleManipulateDoctors();
		} else {
			return handleEditDoctorValidation(doctorWarnings, doctor);
		}
	}

	// Manager Diagnosticlab

	@RequestMapping("/admin/manipulateDiagnosticlabManager.htm")
	public ModelAndView handlemanipulateDiagnosticlabManager(
			@RequestParam("checkedDiagnosticlabKrsnumber") String krs) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("manager", this.managerdiagnosticlabManipulator
				.getByKrsnumber(krs));
		myModel.put("krs", krs);

		return new ModelAndView("admin/manipulateDiagnosticlabManager",
				"model", myModel);
	}

	@RequestMapping("/admin/addDiagnosticlabManagerHome.htm")
	public ModelAndView handleAddDiagnosticlabManagerHome(
			@RequestParam("krs") String krs) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("diagnosticlab", this.diagnosticlabManipulator
				.getByKrsnumber(krs));

		return new ModelAndView("admin/addDiagnosticlabManagerHome", "model",
				myModel);
	}

	@RequestMapping("/admin/addDiagnosticlabManager.htm")
	public ModelAndView handleAddDiagnosticlabManager(
			// parametry dotyczace person
			@RequestParam("addPersonName") String name,
			@RequestParam("addPersonSurname") String surname,
			@RequestParam("addPersonLogin") String login,
			@RequestParam("addPersonPassword") String password,
			@RequestParam("addPersonEmail") String email,
			@RequestParam("addPersonEnabled") String enabled,

			@RequestParam("krs") String krs,
			@RequestParam("addManagerNip") String nip) {

		ManagerWarnings managerWarnings = new ManagerWarnings(Validation
				.validateNip(nip), Validation.validateEmail(email));

		Person person = new Person();

		person.setName(name);
		person.setSurname(surname);
		person.setLogin(login);
		person.setPassword(password);
		person.setEmail(email);
		person.setAuthority("ROLE_DIAGNOSTIC_MANAGER");

		if (enabled.equals("off")) {
			person.setEnabled(false);
		} else {
			person.setEnabled(true);
		}

		Managerdiagnosticlab managerDiagnosticlab = new Managerdiagnosticlab();
		managerDiagnosticlab.setNip(nip);

		Diagnosticlab diagnosticlab = this.diagnosticlabManipulator
				.getByKrsnumber(krs);

		if (managerWarnings.isEmpty()) {

			this.managerdiagnosticlabManipulator.create(managerDiagnosticlab,
					person, diagnosticlab);

			return handlemanipulateDiagnosticlabManager(krs);
		} else {
			managerDiagnosticlab.setPerson(person);
			managerDiagnosticlab.setDiagnosticlab(diagnosticlab);
			return handleaddDiagnosticlabManagerHomeValidation(managerWarnings,
					managerDiagnosticlab);
		}
	}

	@RequestMapping("/admin/addDiagnosticlabManagerHomeValidation.htm")
	public ModelAndView handleaddDiagnosticlabManagerHomeValidation(
			@RequestParam("managerWarnings") ManagerWarnings managerWarnings,
			@RequestParam("managerdiagnosticlab") Managerdiagnosticlab managerdiagnosticlab) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();
		myModel.put("managerdiagnosticlab", managerdiagnosticlab);
		myModel.put("diagnosticlab", this.diagnosticlabManipulator
				.getByKrsnumber(managerdiagnosticlab.getDiagnosticlab()
						.getKrsnumber()));

		myModel.put("managerWarnings", managerWarnings);

		return new ModelAndView("admin/addDiagnosticlabManagerHome", "model",
				myModel);
	}

	// edycja diagnosticlab managera

	@RequestMapping("/admin/editDiagnosticlabManagerHome.htm")
	public ModelAndView handleEditDiagnosticlabManagerHome(
			@RequestParam("krs") String krs) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("manager", this.managerdiagnosticlabManipulator
				.getByKrsnumber(krs));
		myModel.put("diagnosticlab", this.diagnosticlabManipulator
				.getByKrsnumber(krs));

		return new ModelAndView("admin/editDiagnosticlabManagerHome", "model",
				myModel);
	}

	@RequestMapping("/admin/editDoctorValidation.htm")
	public ModelAndView handleEditDoctorValidation(
			@RequestParam("managerWarnings") ManagerWarnings managerWarnings,
			@RequestParam("manager") Managerdiagnosticlab managerdiagnosticlab) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("managerWarnings", managerWarnings);
		myModel.put("manager", managerdiagnosticlab);
		myModel.put("diagnosticlabs", managerdiagnosticlab.getDiagnosticlab());

		return new ModelAndView("admin/editDiagnosticlabManagerHome", "model", myModel);
	}

	@RequestMapping("/admin/changeManagerDiagnosticlabData.htm")
	public ModelAndView handleChangeManagerDiagnosticlabData(
			@RequestParam("editPersonName") String name,
			@RequestParam("editPersonSurname") String surname,
			@RequestParam("editPersonLogin") String login,
			@RequestParam("editPersonEmail") String email,
			@RequestParam("editPersonEnabled") String enabled,

			@RequestParam("editDoctorNip") String nip) {

		ManagerWarnings managerWarnings = new ManagerWarnings(Validation
				.validateNip(nip), Validation.validateEmail(email));

		Managerdiagnosticlab managerdiagnosticlab = this.managerdiagnosticlabManipulator
				.getByNip(nip);

		managerdiagnosticlab.getPerson().setName(name);
		managerdiagnosticlab.getPerson().setSurname(surname);
		managerdiagnosticlab.getPerson().setLogin(login);
		managerdiagnosticlab.getPerson().setEmail(email);

		if (enabled.equals("off")) {
			managerdiagnosticlab.getPerson().setEnabled(false);
		} else {
			managerdiagnosticlab.getPerson().setEnabled(true);
		}
		if (managerWarnings.isEmpty()) {
			this.managerdiagnosticlabManipulator.update(managerdiagnosticlab);
			return handlemanipulateDiagnosticlabManager(managerdiagnosticlab
					.getDiagnosticlab().getKrsnumber());
		} else {
			return handleEditDoctorValidation(managerWarnings,
					managerdiagnosticlab);
		}
	}

	// koniec edycji diagnosticlab managera

	// Koniec Mangera
}
